from datetime import datetime
from typing import Optional
from uuid import UUID, uuid4

from bisheng.database.base import session_getter
from bisheng.database.models.base import SQLModelSerializable
from sqlalchemy import Column, DateTime, String, Text, text, desc
from sqlmodel import Field, SQLModel


class FaultWorkPlanBase(SQLModelSerializable):
    id: UUID = Field(nullable=False, primary_key=True, description='唯一ID')
    group_id: str = Field(sa_column=Column(String(length=255), default=''), description='告警记录ID')
    type: str = Field(sa_column=Column(String(length=255), default=''), description='类型')
    construction_level: str = Field(sa_column=Column(String(length=255),default=''),description='施工等级')
    work_kind: str = Field(sa_column=Column(String(length=255),default=''),description='作业类别')
    work_date: str = Field(sa_column=Column(String(255),default=''),description='作业日期')
    work_time: str = Field(sa_column=Column(String(255),default=''),description='作业时间')
    construction_manager: str = Field(sa_column=Column(String(length=255),default=''),description='施工负责人')
    reporter: str = Field(sa_column=Column(String(length=255),default=''),description='上报人')
    work_department:str = Field(sa_column=Column(String(length=255),default=''),description='作业部门')
    phone: str = Field(sa_column=Column(String(length=255),default=''),description='联系电话')
    work_content: str = Field(sa_column=Column(Text,default=''),description='作业内容')
    station: str = Field(sa_column=Column(String(length=255),default=''),description='区间车站')
    power_area: str = Field(sa_column=Column(String(length=255),default=''),description='供电区域')
    request_area: str = Field(sa_column=Column(String(length=255),default=''),description='请点地点')
    response_area: str = Field(sa_column=Column(String(length=255),default=''),description='销点地点')
    safeguards: str = Field(sa_column=Column(String(length=255),default=''),description='防护措施')
    declare_reason: str = Field(sa_column=Column(String(length=255),default=''),description='申报理由')
    remark: str = Field(sa_column=Column(Text,default=''),description='备注')
    code: str = Field(sa_column=Column(String(length=255), default=''), description='编号')
    create_time: Optional[datetime] = Field(
        sa_column=Column(DateTime, nullable=False, server_default=text('CURRENT_TIMESTAMP')))


class FaultWorkPlan(FaultWorkPlanBase, table=True):
    id: UUID = Field(default_factory=uuid4, primary_key=True, unique=True)


class FaultWorkPlanDao:
    @staticmethod
    def create_fault_work_plan(fault_work_plan:FaultWorkPlan) -> FaultWorkPlan:
        """
        创建新的故障工单
        :param fault_work_plan: FaultWorkPlan 对象
        :return: 创建并保存到数据库的 FaultWorkPlan 对象
        """
        with session_getter() as session:
            # 确保 fault_work_plan 有一个 UUID
            if not fault_work_plan.id:
                fault_work_plan.id = uuid4()
            session.add(fault_work_plan)
            session.commit()
            session.refresh(fault_work_plan)
            return fault_work_plan

    @staticmethod
    def get_fault_work_plan_by_id(id):
        with session_getter() as session:
            if isinstance(id, str):
                id = UUID(id)
            query = session.query(FaultWorkPlan).filter(FaultWorkPlan.id == id)
            return query.first()

    @staticmethod
    def get_lastest_plan():
        with session_getter() as session:
            query = session.query(FaultWorkPlan).order_by(desc(FaultWorkPlan.create_time))
            return query.first()

    @staticmethod
    def get_lastest_plan_by_group_id(group_id):
        with session_getter() as session:
            query = session.query(FaultWorkPlan).filter(FaultWorkPlan.group_id == group_id).order_by(
                desc(FaultWorkPlan.create_time))
            return query.first()
